package org.twndmv.it;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.twndmv.bo.Color;
import org.twndmv.bo.Sex;
import org.twndmv.dto.PersonDTO;
import org.twndmv.dto.PhysicalDetailsDTO;
import org.twndmv.dto.VehicleRegistrationDTO;
import org.twndmv.ejb.DmvTestUtilRemote;
import org.twndmv.ejb.PersonMgmtRemote;
import org.twndmv.ejb.VehicleMgmtRemote;

public class VehicleMgmtIT {
	private static Log log = LogFactory.getLog(VehicleMgmtIT.class);
	private InitialContext jndi;

	private static final String dmvVehicleJNDI = System.getProperty("jndi.name.vehicle",
            "twnDmvEAR/twnDmvEJB/VehicleMgmtEJB!org.twndmv.ejb.VehicleMgmtRemote");
	private static final String dmvTestUtilJNDI = System.getProperty("jndi.name.dmvTestUtil",
            "twnDmvEAR/twnDmvEJB/DmvTestUtilEJB!org.twndmv.ejb.DmvTestUtilRemote");
	private static final String dmvPersonJNDI = System.getProperty("jndi.name.person",
            "twnDmvEAR/twnDmvEJB/PersonMgmtEJB!org.twndmv.ejb.PersonMgmtRemote");
	
	private DmvTestUtilRemote testUtil;
    private VehicleMgmtRemote vehicleManager;
    private PersonMgmtRemote personManager;
    
    @Before
	public void setUp() throws Exception {

	        jndi = new InitialContext();
	        jndi.lookup("/"); //do a quick comms check of JNDI

	        testUtil = (DmvTestUtilRemote)jndi.lookup(dmvTestUtilJNDI);    
	        vehicleManager = (VehicleMgmtRemote)jndi.lookup(dmvVehicleJNDI);
	        personManager = (PersonMgmtRemote)jndi.lookup(dmvPersonJNDI);

	        assertNotNull(testUtil);
	        assertNotNull(vehicleManager);
	        assertNotNull(personManager);
	}
    
    @Test
    public void testVehicleManager() {
    	log.info("===== VehicleMgmtIT.testVehicleManager =====");
    	testUtil.resetAll();
    	
    	PersonDTO testPerson = new PersonDTO();
	    testPerson.setFirstName("Steve");
	    testPerson.setMiddleName("Danger");
	    testPerson.setLastName("Jobs");
	    PhysicalDetailsDTO testDetails = new PhysicalDetailsDTO();
	    testDetails.setHeight(64);
	    testDetails.setWeight(180);
	    testDetails.setSEX(Sex.MALE);
	    testPerson.setPhysicalDetails(testDetails);
	    personManager.createPerson(testPerson);
    	
    	VehicleRegistrationDTO newCar = new VehicleRegistrationDTO();
    	newCar.setMake("WEIRD");
    	newCar.setModel("INTERESTING");
    	newCar.setYear(1987);
    	newCar.setColor(Color.BRONZE);
    
    	
        testPerson = personManager.getPeopleByLastName("Jobs").get(0);
        newCar.getOwners().add(testPerson);
    	vehicleManager.addRegistration(newCar);
    	List<VehicleRegistrationDTO> newCars = vehicleManager.getRegistrationsByMakeModel("WEIRD", "INTERESTING");
    	assertNotNull(newCars);
    	assertEquals(1, newCars.size());
    	VehicleRegistrationDTO tempCar = newCars.get(0);
    	assertEquals(tempCar.getYear(), newCar.getYear());
    	assertTrue(tempCar.getColor().getName().equalsIgnoreCase(newCar.getColor().getName()));
    	log.info("\n\n\nTest car: " + tempCar.getId() + " - " + tempCar.printSummary());
    	testPerson.getRegistrations().add(tempCar);
    	personManager.updatePerson(testPerson);
    	
    	
    	List<PersonDTO> peeps = personManager.getPeopleByLastName(testPerson.getLastName());
    	assertNotNull(peeps);
    	assertEquals(1, peeps.size());
    	PersonDTO resultPeep = peeps.get(0);
    	log.info("\n\n\nTest Person: " + resultPeep.getId() + " - " + resultPeep.printName());
    	
    	List<VehicleRegistrationDTO> cars = vehicleManager.getRegistrationsByOwner(resultPeep);
    	assertNotNull(cars);
    	assertEquals(1,cars.size());
    	VehicleRegistrationDTO resultCar = cars.get(0);
    	assertEquals(resultCar.getYear(), newCar.getYear());
    	assertTrue(resultCar.getColor().getName().equalsIgnoreCase(newCar.getColor().getName()));
    	
    	
    }
	
}
